﻿#include "FITKNastranAbstractAdaptor.h"
#include <QRegExp>

namespace Nastran
{

FITKNastranAbstractAdaptor::~FITKNastranAbstractAdaptor()
{
}

// Set the BDF reader for the FITKNastranAbstractAdaptor
void FITKNastranAbstractAdaptor::setReader(FITKNastranBDFReader *reader)
{
    _reader = reader;
}

// Set the helper for the FITKNastranAbstractAdaptor
void FITKNastranAbstractAdaptor::setReaderHelper(InpNastranReaderHelper *helper)
{
    _readerHelper = helper;
}

void FITKNastranAbstractAdaptor::setWriter(FITKNastranBDFWriter * writer)
{
    _writer = writer;
}

void FITKNastranAbstractAdaptor::setWriterHelper(InpNastranWriterHelper * helper)
{
    _writerHelper = helper;

}

/**
 * @details
 * The getHelper function is a simple getter that returns the pointer to
 * the InpNastranReaderHelper object stored in the private member variable
 * _helper of the FITKNastranAbstractAdaptor class.
 * This helper object is likely used for reading and interpreting Nastran
 * input files. By retrieving this helper, other parts of the program can
 * utilize the same helper to maintain consistency in reading and interpreting
 * Nastran data. Proper usage of this function ensures that the helper's
 * functionalities are accessible where needed, facilitating the smooth
 * processing of Nastran-related tasks.
 */
InpNastranReaderHelper *FITKNastranAbstractAdaptor::getReaderHelper()
{
    return _readerHelper;
}

// Retrieve the BDF reader from the FITKNastranAbstractAdaptor
FITKNastranBDFReader *FITKNastranAbstractAdaptor::getReader()
{
    return _reader;
}

InpNastranWriterHelper * FITKNastranAbstractAdaptor::getWriterHelper()
{
    return _writerHelper;
}

FITKNastranBDFWriter * FITKNastranAbstractAdaptor::getWriter()
{
    return _writer;
}

double FITKNastranAbstractAdaptor::String2Value(const QString& string)
{
    if (string.isEmpty()) return 0;
    bool ok = false;
    double v = string.toDouble(&ok);
    if (ok) return v;

    QString s = string.simplified();
    QRegExp pattern("[\\+\\-]");
    const int index = pattern.lastIndexIn(string);
    //开头位置匹配为0
    if (index < 1) return 0;
     
    s.insert(index, "e");
    v = s.toDouble(&ok);
    if (ok) return v;
    return 0;

}

QString FITKNastranAbstractAdaptor::Value2String(double d, int prec, int width1, char f)
{
    QRegExp reg;
    QString strNumber = QString("%1").arg(d, 0, f, prec);
    QString rtStr = strNumber;
    //判断0 
    if (fabs(d) < 1e-16)
    {
        strNumber = "0.";
    }
    else if (f == 'g' && (fabs(d) >= 1e+2 || fabs(d) <= 1e-2))
    {
        strNumber = QString("%1").arg(d, 0, 'e', prec - 1);
    }

    if (strNumber.contains("e"))
    {
        reg.setPattern("0+\\e");
        rtStr = strNumber.replace(reg, "e");
    }
    else if (strNumber.contains("E"))
    {
        reg.setPattern("0+\\E");
        rtStr = strNumber.replace(reg, "e");
    }
    else if (!strNumber.contains("."))
    {
        rtStr = strNumber + ".";
    }
    else
    {
        reg.setPattern("0+$");
        rtStr = strNumber.replace(reg, "");
    }

    rtStr = rtStr.replace(".e", "e");
    //bdf特殊处理
    if (rtStr.size() > 8 && rtStr.mid(0, 3) == "-0.")
        rtStr = rtStr.replace("-0.", "-.");
    if (rtStr.size() > 8 && rtStr.mid(0, 2) == "0.")
        rtStr = rtStr.replace("0.", ".");
    if (rtStr.size() > 8 && rtStr.contains("e-0"))
        rtStr = rtStr.replace("e-0", "-");
    if (rtStr.contains("e"))
        rtStr = rtStr.replace("e", "");
    rtStr = rtStr.rightJustified(width1, ' ');

    return rtStr;
}

} // namespace Nastran